﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmDrawList
    Public Primary_Key As Integer = -1
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
#Region "Functioin"
    Sub Load_Data()
        Try
            Strsql = "Select doc_id,doc_no,doc_date,doc_type.doc_type_name,dep_name,status_name,doc_lastupdate from document_draw_header "
            Strsql = Strsql & " left join departments on document_draw_header.doc_department = departments.dep_id"
            Strsql = Strsql & " left join doc_status on document_draw_header.doc_status = doc_status.status_id "
            Strsql = Strsql & " left join doc_type on document_draw_header.doc_type = doc_type.doc_type_id"
            Strsql = Strsql & " order by doc_id desc"
            Dim DT_Doc_Draw_Header As DataTable
            DT_Doc_Draw_Header = _mysql.GetMYSQLDataTable(Strsql, "DT_Doc_Draw_Header")
            With GridControl1
                .DataSource = DT_Doc_Draw_Header
                Col_doc_id.FieldName = "doc_id"
                If DT_Doc_Draw_Header.Rows.Count = 0 Then
                    BtnDelete.Enabled = False
                    BtnEdit.Enabled = False
                Else
                    BtnEdit.Enabled = True
                    BtnDelete.Enabled = True
                End If
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Function Get_NEW_ID() As Integer
        Try
            'Get Last Insert 
            Strsql = "SELECT LAST_INSERT_ID();"
            Dim Last_ITEM_ID As Integer = 0
            Get_NEW_ID = _mysql.MySQLExecuteScalar(Strsql)

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Get_NEW_ID"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With

        End Try
    End Function
#End Region
    Private Sub frmReceiveList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Data()
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        With frmNewDrawForm
            .MdiParent = frmmain
            .WindowState = FormWindowState.Maximized
            .Show()
        End With
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If MsgBox("คุณต้องการลบข้อมูล 'ใบรับสินค้า' รหัส " & Primary_Key & " ออกจากระบบหรือไม่?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub

        Try
            Strsql = "delete from document_draw_detail where doc_detail_id = " & Primary_Key
            If _mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnDelete_Click : delete detail"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
                Primary_Key = -1

            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnDelete_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

        Try
            Strsql = "delete from document_draw_header where doc_id = " & Primary_Key
            If _mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnDelete_Click : delete header"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
            Else
                MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบ")
                Load_Data()
            End If

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnDelete_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try



    End Sub

    Private Sub GridControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridControl1.Click

    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try
            Dim Gv As GridView = sender
            Primary_Key = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("doc_id"))

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        If Primary_Key = -1 Then Exit Sub
        With frmEditDrawForm
            .Primary_Key = Primary_Key
            .MdiParent = frmmain
            .WindowState = FormWindowState.Maximized
            .Show()
            Me.Close()
        End With
    End Sub

    Private Sub BtnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRefresh.Click
        Load_Data()
    End Sub


    Private Sub BtnClone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClone.Click
        If MsgBox("คุณต้องคัดลอกข้อมูลสินค้า Record ที่ " & Primary_Key & " เป็น Record ใหม่ใช่หรือไม่?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการคัดลอก") = MsgBoxResult.No Then Exit Sub

        Try
            Strsql = "Insert into document_draw_header (doc_no,doc_date,doc_department,doc_remarks,doc_recevie_date,doc_lastupdate,doc_status,doc_type,doc_place,doc_staff) "
            Strsql = Strsql & " select  doc_no,doc_date,doc_department,doc_remarks,doc_recevie_date,doc_lastupdate,doc_status,doc_type,doc_place,doc_staff FROM document_draw_header where doc_id = " & Primary_Key
            'Strsql = "delete from document_draw_detail where doc_detail_id = " & Primary_Key
            If _mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnClone_Click : Copy detail"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                    Exit Sub
                End With


            End If

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnClone_Click : Copy header"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
      


        Dim DT_Document_draw_detail As New DataTable
        Strsql = "Select * from document_draw_detail   where doc_head_id = " & Primary_Key
        DT_Document_draw_detail = _mysql.GetMYSQLDataTable(Strsql, "document_draw_detail")
        'For Each DR As DataRow In DT_Document_draw_detail.Rows
        Try
            Strsql = "Insert into document_draw_detail (doc_head_id,doc_rowid,productname,serialno,band,model,totalpriceprice,drawqty,receiveqty,staff,remarks,purchase_date,mdf_date,warantry,priceincludevat,vat,priceperunit,nonprice,nonmfd,nonbuydate,nonvat,imagepart) "
            Strsql = Strsql & " select  " & Get_NEW_ID() & " as doc_head_id,doc_rowid,productname,serialno,band,model,totalpriceprice,drawqty,receiveqty,staff,remarks,purchase_date,mdf_date,warantry,priceincludevat,vat,priceperunit,nonprice,nonmfd,nonbuydate,nonvat,imagepart FROM document_draw_detail where doc_head_id = " & Primary_Key



            'Strsql = "delete from document_draw_detail where doc_detail_id = " & Primary_Key
            If _mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnClone_Click : Copy detail"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                    Exit Sub
                End With


            End If

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnClone_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
        ' Next

        Load_Data()

    End Sub
End Class